import { login, logout, getUserInfo, getTableMsg, getOptionsMsg, getaskTableMsg, getminTableMsg, adTableMsg, deleteTablemsg, importFile, getSearchMsg, getaskSearchMsg, getExpiretaskMsg,addTaskTablemsg} from '@/api/user'
import { setToken, getToken } from '@/libs/util'

export default {
  state: {
    userName: '',
    userId: '',
    avatorImgPath: '',
    token: getToken(),
    access: '',
    tablemsg: {},
    asktablemsg: {},
    getmintablemsg: {},
    adTableMsg: 0,
    deleteTableMsg: 0,
    getoptionsmsg: {},
    getaddTablemsg:0
  },
  mutations: {
    setAvator (state, avatorPath) {
      state.avatorImgPath = avatorPath
    },
    setUserId (state, id) {
      state.userId = id
    },
    setUserName (state, name) {
      state.userName = name
    },
    setAccess (state, access) {
      state.access = access
    },
    setToken (state, token) {
      state.token = token
      setToken(token)
    },
    setTablemsg (state, tablemsg) {
      state.tablemsg = tablemsg || {}
    },
    setaskTablemsg (state, asktablemsg) {
      state.asktablemsg = asktablemsg || {}
    },
    setminTablemsg (state, mintablemsg) {
      state.getmintablemsg = mintablemsg || {}
    },
    setadTableMsg (state) {
      state.adTableMsg = 1
    },
    setdeleteTablemsg (state) {
      state.deleteTableMsg = 1
    },
    setOptionsmsg (state, optionsmsg) {
      state.getoptionsmsg = optionsmsg || {}
    },
    setaddTablemsg(state) {
      state.getaddTablemsg = 1
    },
  },
  actions: {
    // 登录
    handleLogin ({ commit }, {userName, password}) {
      userName = userName.trim()
      return new Promise((resolve, reject) => {
        login({
          userName,
          password
        }).then(res => {
          const data = res.data
          commit('setToken', data.token)
          resolve()
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 退出登录
    handleLogOut ({ state, commit }) {
      return new Promise((resolve, reject) => {
        // logout(state.token).then(() => {
        //   commit('setToken', '')
        //   commit('setAccess', [])
        //   resolve()
        // }).catch(err => {
        //   reject(err)
        // })
        // 如果你的退出登录无需请求接口，则可以直接使用下面三行代码而无需使用logout调用接口
        commit('setToken', '')
        commit('setAccess', [])
        resolve()
      })
    },
    // 获取用户相关信息
    getUserInfo ({ state, commit }) {
      return new Promise((resolve, reject) => {
        //
        getUserInfo(state.token).then((res) => {
          let data = res.data || {}
          commit('setAvator', data.avator)
          commit('setUserName', data.user_name)
          commit('setUserId', data.user_id)
          commit('setAccess', data.access)
          resolve(data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    getTableMsg: ({commit, state}, {vue, param}) => {
      return new Promise((resolve, reject) => {
        getTableMsg(state.token, param).then((res) => {
          let data = res.data || {}
          commit('setTablemsg', data)
          resolve(data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    getOptionsMsg: ({commit, state}, {vue}) => {
      return new Promise((resolve, reject) => {
        getOptionsMsg(state.token).then((res) => {
          let data = res.data || {}
          commit('setOptionsmsg', data)
          resolve(data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    getaskTableMsg: ({commit, state}, {vue, param}) => {
      return new Promise((resolve, reject) => {
        getaskTableMsg(state.token, param).then((res) => {
          let data = res.data || {}
          commit('setaskTablemsg', data)
          resolve(data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    getminTableMsg: ({commit, state}, {vue, param}) => { // 项目定义
      return new Promise((resolve, reject) => {
        getminTableMsg(state.token, param).then((res) => {
          let data = res.data || {}
          commit('setminTablemsg', data)
          resolve(data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    adTableMsg: ({commit, state}, {vue, param}) => { // 新增or修改
      return new Promise((resolve, reject) => {
        adTableMsg(state.token, param).then((res) => {
          let data = res.data || {}
          commit('setadTableMsg', data)
          resolve(data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    deleteTablemsg: ({commit, state}, {vue, param}) => { // 新增or修改
      return new Promise((resolve, reject) => {
        deleteTablemsg(state.token, param).then((res) => {
          let data = res.data || {}
          commit('setdeleteTablemsg', data)
          resolve(data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    getSearchMsg: ({commit, state}, {vue, param}) => {
      return new Promise((resolve, reject) => {
        getSearchMsg(state.token, param).then((res) => {
          let data = res.data || {}
          commit('setTablemsg', data)
          resolve(data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    getaskSearchMsg: ({commit, state}, {vue, param}) => {
      return new Promise((resolve, reject) => {
        getaskSearchMsg(state.token, param).then((res) => {
          let data = res.data || {}
          commit('setaskTablemsg', data)
          resolve(data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    getExpiretaskMsg: ({commit, state}, {vue, param}) => {
      return new Promise((resolve, reject) => {
        getExpiretaskMsg(state.token, param).then((res) => {
          let data = res.data || {}
          commit('setTablemsg', data)
          resolve(data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    addTaskTablemsg: ({commit, state}, {vue, param}) => { // task新增
      debugger
      return new Promise((resolve, reject) => {
        addTaskTablemsg(state.token, param).then((res) => {
          if(res!==false){
            debugger
            let data = res.data || {}
            commit('setaddTablemsg', data)
            resolve(data)
          }
        }).catch(err => {
          reject(err)
        })
      })
    },

  }
}
